﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace DALL.LibrarySys
{
    /// <summary>
    /// 数据访问类:T_PUB_LIBRARYOUT
    /// </summary>
    public class T_PUB_LIBRARYOUT
    {
        public T_PUB_LIBRARYOUT()
        { }

        public DataSet GetApplyTitel(int branch_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITEL FROM T_PUB_LIBRARYOUT ");
            strSql.Append(" where BRANCH_ID=" + branch_id);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetApplyTitel()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITEL FROM T_PUB_LIBRARYOUT ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_PUB_LIBRARYOUT where ID=@ID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, ID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }



        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_LIBRARYOUT ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.LibrarySys.T_PUB_LIBRARYOUT model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_LIBRARYOUT(");
            strSql.Append("ID,BRANCH_ID,LIBRARY_ID,APPLICANT,APPLY_TIMEOUT,REAL_TIMEOUT,PLAN_TIMEIN,REAL_TIMEIN,STATE,REMARK,TITEL,APPLY_TIME,ORDER_ID,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME)");

            strSql.Append(" values (");
            strSql.Append("@ID,@BRANCH_ID,@LIBRARY_ID,@APPLICANT,@APPLY_TIMEOUT,@REAL_TIMEOUT,@PLAN_TIMEIN,@REAL_TIMEIN,@STATE,@REMARK,@TITEL,@APPLY_TIME,@ORDER_ID,@NOTATION,@APPROVER,@APPLICANT_NAME,@APPROVER_NAME)");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, model.ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, model.LIBRARY_ID);
            db.AddInParameter(dbCommand, "APPLICANT", DbType.Int32, model.APPLICANT);
            db.AddInParameter(dbCommand, "APPLY_TIMEOUT", DbType.DateTime, model.APPLY_TIMEOUT);
            db.AddInParameter(dbCommand, "REAL_TIMEOUT", DbType.DateTime, model.REAL_TIMEOUT);
            db.AddInParameter(dbCommand, "PLAN_TIMEIN", DbType.DateTime, model.PLAN_TIMEIN);
            db.AddInParameter(dbCommand, "REAL_TIMEIN", DbType.DateTime, model.REAL_TIMEIN);
            db.AddInParameter(dbCommand, "STATE", DbType.String, model.STATE);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "TITEL", DbType.String, model.TITEL);
            db.AddInParameter(dbCommand, "APPLY_TIME", DbType.DateTime, model.APPLY_TIME);
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.Int32, model.ORDER_ID);
            db.AddInParameter(dbCommand, "NOTATION", DbType.String, model.NOTATION);
            db.AddInParameter(dbCommand, "APPROVER", DbType.Int32, model.APPROVER);
            db.AddInParameter(dbCommand, "APPLICANT_NAME", DbType.String, model.APPLICANT_NAME);
            db.AddInParameter(dbCommand, "APPROVER_NAME", DbType.String, model.APPROVER_NAME);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.LibrarySys.T_PUB_LIBRARYOUT model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_LIBRARYOUT set ");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("LIBRARY_ID=@LIBRARY_ID,");
            strSql.Append("APPLICANT=@APPLICANT,");
            strSql.Append("APPLY_TIMEOUT=@APPLY_TIMEOUT,");
            strSql.Append("REAL_TIMEOUT=@REAL_TIMEOUT,");
            strSql.Append("PLAN_TIMEIN=@PLAN_TIMEIN,");
            strSql.Append("REAL_TIMEIN=@REAL_TIMEIN,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("REMARK=@REMARK,");
            strSql.Append("TITEL=@TITEL,");
            strSql.Append("APPLY_TIME=@APPLY_TIME,");
            strSql.Append("ORDER_ID=@ORDER_ID,");
            strSql.Append("NOTATION=@NOTATION,");
            strSql.Append("APPROVER=@APPROVER,");
            strSql.Append("APPLICANT_NAME=@APPLICANT_NAME,");
            strSql.Append("APPROVER_NAME=@APPROVER_NAME");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, model.ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, model.LIBRARY_ID);
            db.AddInParameter(dbCommand, "APPLICANT", DbType.Int32, model.APPLICANT);
            db.AddInParameter(dbCommand, "APPLY_TIMEOUT", DbType.DateTime, model.APPLY_TIMEOUT);
            db.AddInParameter(dbCommand, "REAL_TIMEOUT", DbType.DateTime, model.REAL_TIMEOUT);
            db.AddInParameter(dbCommand, "PLAN_TIMEIN", DbType.DateTime, model.PLAN_TIMEIN);
            db.AddInParameter(dbCommand, "REAL_TIMEIN", DbType.DateTime, model.REAL_TIMEIN);
            db.AddInParameter(dbCommand, "STATE", DbType.String, model.STATE);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "TITEL", DbType.String, model.TITEL);
            db.AddInParameter(dbCommand, "APPLY_TIME", DbType.DateTime, model.APPLY_TIME);
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.Int32, model.ORDER_ID);
            db.AddInParameter(dbCommand, "NOTATION", DbType.String, model.NOTATION);
            db.AddInParameter(dbCommand, "APPROVER", DbType.Int32, model.APPROVER);
            db.AddInParameter(dbCommand, "APPLICANT_NAME", DbType.String, model.APPLICANT_NAME);
            db.AddInParameter(dbCommand, "APPROVER_NAME", DbType.String, model.APPROVER_NAME);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdatePass(string id, string state, string notation, int approver)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_LIBRARYOUT set ");
            strSql.Append("STATE=@STATE,");
            strSql.Append("NOTATION=@NOTATION,");
            strSql.Append("APPROVER=@APPROVER");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, id);
            db.AddInParameter(dbCommand, "STATE", DbType.String, state);
            db.AddInParameter(dbCommand, "NOTATION", DbType.String, notation);
            db.AddInParameter(dbCommand, "APPROVER", DbType.Int32, approver);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateOut(string id , string state, DateTime outdate)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_LIBRARYOUT set ");
            strSql.Append("STATE=@STATE,");
            strSql.Append("REAL_TIMEOUT=@REAL_TIMEOUT");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, id);
            db.AddInParameter(dbCommand, "STATE", DbType.String, state);
            db.AddInParameter(dbCommand, "REAL_TIMEOUT", DbType.DateTime, outdate);
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateIn(string id, string state, DateTime outdate)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_LIBRARYOUT set ");
            strSql.Append("STATE=@STATE,");
            strSql.Append("REAL_TIMEIN=@REAL_TIMEIN");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, id);
            db.AddInParameter(dbCommand, "STATE", DbType.String, state);
            db.AddInParameter(dbCommand, "REAL_TIMEIN", DbType.DateTime, outdate);
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.LibrarySys.T_PUB_LIBRARYOUT GetModel(string ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,LIBRARY_ID,APPLICANT,APPLY_TIMEOUT,REAL_TIMEOUT,PLAN_TIMEIN,REAL_TIMEIN,STATE,REMARK,TITEL,APPLY_TIME,ORDER_ID,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME from T_PUB_LIBRARYOUT ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, ID);
            Model.LibrarySys.T_PUB_LIBRARYOUT model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,LIBRARY_ID,APPLICANT,APPLY_TIMEOUT,REAL_TIMEOUT,PLAN_TIMEIN,REAL_TIMEIN,STATE,REMARK,TITEL,APPLY_TIME,ORDER_ID,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME ");
            strSql.Append(" FROM T_PUB_LIBRARYOUT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetApplyList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,a.BRANCH_ID,BRANCH_NAME,a.LIBRARY_ID,LIBRARY_NAME,APPLICANT,APPLY_TIMEOUT,REAL_TIMEOUT,PLAN_TIMEIN,REAL_TIMEIN,STATE,a.REMARK,TITEL,APPLY_TIME,ORDER_ID,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME ");
            strSql.Append(" FROM T_PUB_LIBRARYOUT as a, T_PUB_LIBRARY as b, T_PUB_BRANCH as c");
            strSql.Append(" where  b.LIBRARY_ID = a.LIBRARY_ID and c.BRANCH_ID = a.BRANCH_ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetApplyInList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.ID,a.BRANCH_ID,BRANCH_NAME,a.LIBRARY_ID,LIBRARY_NAME,APPLICANT,APPLY_TIMEOUT,REAL_TIMEOUT,PLAN_TIMEIN,REAL_TIMEIN,STATE,a.REMARK,TITEL,APPLY_TIME,ORDER_ID,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME,d.ID as LIBRARYOUT_MATERIAL_ID,MATERIAL_ID,MATERIAL_NAME,NUM,MATERIAL_STATE,RETURN_NUM,DISCARD_ID,TIMEIN ");
            strSql.Append(" FROM T_PUB_LIBRARYOUT as a, T_PUB_LIBRARY as b, T_PUB_BRANCH as c, T_PUB_LIBRARYOUTMATERIAL as d");
            strSql.Append(" where  b.LIBRARY_ID = a.LIBRARY_ID and c.BRANCH_ID = a.BRANCH_ID and d.LIBRARYOUTID = a.ID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.LibrarySys.T_PUB_LIBRARYOUT> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,LIBRARY_ID,APPLICANT,APPLY_TIMEOUT,REAL_TIMEOUT,PLAN_TIMEIN,REAL_TIMEIN,STATE,REMARK,TITEL,APPLY_TIME,ORDER_ID,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME ");
            strSql.Append(" FROM T_PUB_LIBRARYOUT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.LibrarySys.T_PUB_LIBRARYOUT> list = new List<Model.LibrarySys.T_PUB_LIBRARYOUT>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }



        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.LibrarySys.T_PUB_LIBRARYOUT ReaderBind(IDataReader dataReader)
        {
            Model.LibrarySys.T_PUB_LIBRARYOUT model = new Model.LibrarySys.T_PUB_LIBRARYOUT();
            object ojb;
            model.ID = dataReader["ID"].ToString();
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            ojb = dataReader["LIBRARY_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LIBRARY_ID = (int)ojb;
            }
            ojb = dataReader["APPLICANT"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPLICANT = (int)ojb;
            }
            ojb = dataReader["APPLY_TIMEOUT"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPLY_TIMEOUT = (DateTime)ojb;
            }
            ojb = dataReader["REAL_TIMEOUT"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.REAL_TIMEOUT = (DateTime)ojb;
            }
            ojb = dataReader["PLAN_TIMEIN"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PLAN_TIMEIN = (DateTime)ojb;
            }
            ojb = dataReader["REAL_TIMEIN"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.REAL_TIMEIN = (DateTime)ojb;
            }
            model.STATE = dataReader["STATE"].ToString();
            model.REMARK = dataReader["REMARK"].ToString();
            model.TITEL = dataReader["TITEL"].ToString();
            ojb = dataReader["APPLY_TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPLY_TIME = (DateTime)ojb;
            }
            ojb = dataReader["ORDER_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ORDER_ID = (int)ojb;
            }
            model.NOTATION = dataReader["NOTATION"].ToString();
            ojb = dataReader["APPROVER"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPROVER = (int)ojb;
            }
            model.APPLICANT_NAME = dataReader["APPLICANT_NAME"].ToString();
            model.APPROVER_NAME = dataReader["APPROVER_NAME"].ToString();
            return model;
        }

        #endregion  Method
    }
}
